package com.yc.navy.goods.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.bean.FoodPrintInfo;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.IFoodPrintService;
import com.yc.navy.goods.vo.ResultVO;

@RestController
@RequestMapping("/food")
public class FoodPrintInfoController {
	@Autowired
	private IFoodPrintService foodPrintService;
	
	private String getLogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfoDTO.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		MemberInfoDTO m = (MemberInfoDTO) obj;
		return m.getMid() + "";
	}
	
	@RequestMapping("/add")
	public ResultVO add(FoodPrintInfo foot,HttpSession session) {
		String mid = getLogin(session);
		foot.setMid(Integer.valueOf(mid));
		
		Integer result = foodPrintService.check(foot);
		
		if(result == null) {
			Integer result2 = foodPrintService.add(foot);
			if(result2 <= 0) {
				return new ResultVO(ResultEnum.ERROR);
			}
			return new ResultVO(ResultEnum.SUCCESS);
		}
		
		if(result == -1) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.HAVEVALUE);
	}
	
	@RequestMapping("/find")
	public ResultVO find(HttpSession session) {
		FoodPrintInfo foot = new FoodPrintInfo();
		String mid = getLogin(session);
		foot.setMid(Integer.valueOf(mid));
		
		List<FoodPrintInfo>  list= foodPrintService.find(foot);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
}
